import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { loadJS } from "./utils";
import authConfig from "./config";
var AppleIDAuth = /*#__PURE__*/ function _target() {
    "use strict";
    function AppleIDAuth() {
        var _this = this;
        _class_call_check(this, AppleIDAuth);
        _define_property(this, "load", window.AppleID ? true : false);
        _define_property(this, "getCode", function() {
            return new Promise(function(resolve, reject) {
                var _window_AppleID;
                (_window_AppleID = window.AppleID) === null || _window_AppleID === void 0 ? void 0 : _window_AppleID.auth.signIn().then(function(res) {
                    console.log(res);
                    resolve(_object_spread({
                        code: res.authorization.code,
                        state: res.authorization.state
                    }, res.authorization));
                }, reject);
            });
        });
        _define_property(this, "getAccessToken", function() {
            return new Promise(function(resolve, reject) {
                try {
                    _this.getCode().then(function(res) {
                        var code = res.code;
                    });
                } catch (err) {
                    reject(err);
                }
            });
        });
        if ((_instanceof(this, AppleIDAuth) ? this.constructor : void 0) !== AppleIDAuth) {
            return;
        }
        if (!AppleIDAuth._instance) {
            AppleIDAuth._instance = this;
        }
        /** 加载sdk */ this.loadFBSdk();
        return AppleIDAuth._instance;
    }
    _create_class(AppleIDAuth, [
        {
            key: "loadFBSdk",
            value: function loadFBSdk(cb, onError) {
                var _this = this;
                /** apple 的clientID 是 注册登录时使用的网址的倒写 不能更换 测试网页得使用localhost.dev 或者 local.dev */ loadJS("https://cdn.nfkings.com/js/sdks/appleid.cdn-apple.com_appleauth_static_jsapi_appleid_1_en_US_appleid.auth.js", function() {
                    var _window_AppleID, _this1, _this_onLoad;
                    (_window_AppleID = window.AppleID) === null || _window_AppleID === void 0 ? void 0 : _window_AppleID.auth.init({
                        clientId: authConfig.APPLE_APPID,
                        usePopup: true,
                        scope: "name email",
                        state: "apple",
                        nonce: "NONCE",
                        redirectURI: authConfig.REDIRECT_URI
                    });
                    _this.load = true;
                    (_this_onLoad = (_this1 = _this).onLoad) === null || _this_onLoad === void 0 ? void 0 : _this_onLoad.call(_this1);
                    cb === null || cb === void 0 ? void 0 : cb();
                    document.addEventListener("AppleIDSignInOnSuccess", function(event) {
                        var _this1, _this_resolve;
                        (_this_resolve = (_this1 = _this).resolve) === null || _this_resolve === void 0 ? void 0 : _this_resolve.call(_this1, event);
                        _this.resolve = undefined;
                    });
                    document.addEventListener("AppleIDSignInOnFailure", function(event) {
                        var _this1, _this_reject;
                        (_this_reject = (_this1 = _this).reject) === null || _this_reject === void 0 ? void 0 : _this_reject.call(_this1, event);
                        _this.reject = undefined;
                    });
                }, function() {
                    var _this1, _this_onError;
                    onError === null || onError === void 0 ? void 0 : onError({
                        msg: "apple sdk load fail,please try later"
                    });
                    (_this_onError = (_this1 = _this).onError) === null || _this_onError === void 0 ? void 0 : _this_onError.call(_this1);
                });
            }
        },
        {
            key: "init",
            value: function init() {
                var _this = this;
                /** 假如 sdk 加载失败可以使用init 重新加载sdk */ return new Promise(function(resolve, reject) {
                    if (_this.load && window.AppleID) {
                        resolve(AppleIDAuth._instance);
                        return;
                    }
                    _this.loadFBSdk(function() {
                        resolve(AppleIDAuth._instance);
                    }, reject);
                });
            }
        }
    ], [
        {
            key: "getInstance",
            value: function getInstance() {
                if (!AppleIDAuth._instance) {
                    AppleIDAuth._instance = new AppleIDAuth();
                }
                return AppleIDAuth._instance;
            }
        }
    ]);
    return AppleIDAuth;
}();
_define_property(AppleIDAuth, "_instance", null);
export default AppleIDAuth.getInstance();

 //# sourceMappingURL=apple.js.map